package com.example.xsssglxt.controller;

import com.alibaba.excel.EasyExcel;
import com.example.xsssglxt.entity.Live;
import com.example.xsssglxt.entity.Louyu;
import com.example.xsssglxt.entity.Sushe;
import com.example.xsssglxt.mapper.LiveMapper;
import com.example.xsssglxt.service.LiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Result;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Controller
@RequestMapping("/live")
public class LiveController {
    @Autowired
    private LiveMapper liveMapper;
    @Autowired
    private LiveService liveService;
    @GetMapping("/index")
    public Map<String,Object> index(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String studentname,@RequestParam String number){
        pageNum=(pageNum-1)*pageSize;
        Map<String,Object> res=new HashMap<>();
List<Live> data=liveMapper.selectpage(pageNum,pageSize,studentname,number);
        Integer total=liveMapper.selecttotal(studentname,number);
        res.put("data",data);
        res.put("total",total);
        res.put("code",200);
        return res;
    }
    @PostMapping("/add")
    public Map<String,Object> addly(@RequestBody Live live){
        Map<String,Object> res=new HashMap<>();
        if(liveService.live(live) == 1){
            res.put("code",200);
            res.put("msg","操作成功");
        }
        return res;

    }
    @PostMapping("/update")
    public Map<String ,Object> xgly(@RequestBody Live live){
        Map<String,Object> res=new HashMap<>();
        if(liveService.live1(live) == 1){
            res.put("code",200);
            res.put("msg","操作成功");

        }
        return  res;
    }
    @DeleteMapping("/{id}")
    public Map<String,Object> scly(@PathVariable Integer id){
        Map<String,Object> res=new HashMap<>();
        Integer col=liveMapper.deleter(id);
        res.put("data",col);
        return res;

    }
    @GetMapping("/down")
    public Result down(HttpServletResponse response) throws IOException {
        List<Live> studentList=liveMapper.selectlist();
        response.setContentType("application/octet-stream");
        String fileName= URLEncoder.encode("入住表","UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(),Live.class).autoCloseStream(Boolean.FALSE).sheet("入住表").doWrite(studentList);


        return null;
    }
}
